<?php

App::uses('AppController', 'Controller');

class UsersController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('login');
    }

    public function login() {
        $this->layout = 'painel';
        $this->theme = NULL;
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                if ($this->Auth->user('role') == 'customer') {
                    if (trim($this->request->referer(true), '/')) {
                        return $this->redirect($this->referer());
                    }
                    return $this->redirect(array(
                                'controller' => 'users',
                                'action' => 'dashboard',
                                'customer' => true,
                                'admin' => false
                    ));
                } elseif ($this->Auth->user('role') == 'admin') {
                    return $this->redirect(array(
                                'controller' => 'users',
                                'action' => 'dashboard',
                                'manager' => false,
                                'admin' => true
                    ));
                } else {
                    $this->Session->setFlash('Login is incorrect');
                }
            } else {
                $this->Session->setFlash('Login is incorrect');
            }
        }
    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
    }

    /* -------------------------------- Cliente ----------------------------------------------------- */

    public function customer_dashboard() {
        
    }

    /* -------------------------------- Administração ----------------------------------------------------- */

    public function admin_dashboard() {
        
    }

    /**
     * Lista todos os "usuarios".
     *
     * @method        admin_index
     * @version       1.0.0
     * @return void
     */
    public function admin_index() {
        parent::admin_index();
    }

    /**
     * Cadastrar "usuarios"
     *
     * @method        admin_add
     * @version       1.0.0
     * @return void
     */
    public function admin_add() {
        parent::admin_add();
    }

    /**
     * Alterar de acordo com o ID  "usuarios" especifico.
     *
     * @method        admin_edit
     * @version       1.0.0
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        parent::admin_edit($id);
    }

    /**
     * Excluir de acordo com o ID  "usuarios" especifico.
     *
     * @method        admin_delete
     * @version       1.0.0
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        parent::admin_delete($id);
    }

    /**
     * Exportar dados do banco em formato CSV da tabela "usuarios".
     *
     * @method        admin_export_data
     * @version       1.0.0
     * @return void
     */
    public function admin_export() {
        parent::admin_export();
    }

    public function admin_password($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException('Invalid user');
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('The user has been saved');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The user could not be saved. Please, try again.');
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
    }

}
